﻿using System;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Reader.Domain
{
    public class SearchResults
    {
        public List<SearchResult> AllResult
        {
            get;
            set;
        }
    }
    public class SearchResult
    {
        static SearchResult()
        {
            pages.Add(new TypeRegex(PageTypeEnum.BookPage, @"^http://(?<domain>www\.xiaoshuozhe\.com)/book_info/\d+/\d+\.htm$"));
            pages.Add(new TypeRegex(PageTypeEnum.BookContent, @"^http://(?<domain>www\.xiaoshuozhe\.com)/files/article/books/\d+/\d+/?(?:index.html)*$"));
            pages.Add(new TypeRegex(PageTypeEnum.Chapter, @"^http://(?<domain>www\.xiaoshuozhe\.com)/files/article/books/\d+/\d+/\d+\.html$"));
            //理想文学
            pages.Add(new TypeRegex(PageTypeEnum.BookPage, @"^http://(?<domain>www\.lxwxw\.com)/xiaoshuoinfo/\d+/\d+\.html$"));
            pages.Add(new TypeRegex(PageTypeEnum.BookContent, @"^http://(?<domain>www\.lxwxw\.com)/files/article/html/\d+/\d+/?(?:index\.html)?$"));
            pages.Add(new TypeRegex(PageTypeEnum.Chapter, @"^http://(?<domain>www\.lxwxw\.com)/files/article/html/\d+/\d+/\d+\.html$"));
            //qidian
            pages.Add(new TypeRegex(PageTypeEnum.BookPage, @"^http://(?<domain>www\.qidian\.com)/Book/\d*\.aspx$"));
            pages.Add(new TypeRegex(PageTypeEnum.BookContent, @"^http://(?<domain>www\.qidian\.com)/BookReader/\d*\.aspx$"));
            pages.Add(new TypeRegex(PageTypeEnum.Chapter, @"^http://(?<domain>www\.qidian\.com)/BookReader/\d*,\d*.aspx$"));
        }
        public SearchResult()
        {

            
        }
        public string Uri { get; set; }
        public string Content { get; set; }
        public string ViewName
        {
            get
            {
                PageTypeEnum pt = GetPageType(Uri);
                if (pt == PageTypeEnum.None)
                {
                    return "none";
                }
                else if (pt == PageTypeEnum.BookContent)
                {
                    return "content";
                }
                else if (pt == PageTypeEnum.BookPage)
                {
                    return "bookpage";
                }
                else
                {
                    return "chapter";
                }
            }
        }
        public string PageType
        {
            get {
                PageTypeEnum pt = GetPageType(Uri);
                if (pt == PageTypeEnum.None)
                {
                    return "未知";
                }
                else if (pt == PageTypeEnum.BookContent)
                {                    
                    return  "打开目录";
                }
                else if (pt == PageTypeEnum.BookPage)
                {
                    
                    return "打开简介";
                }
                else
                {
                  
                    return "打开章节";
                }
            }
        }
        private static readonly List<TypeRegex> pages = new List<TypeRegex>();
        private PageTypeEnum GetPageType(string uri)
        {
            var t = from p in pages
                    let tp = p
                    where Regex.IsMatch(uri, p.MatchRegex,RegexOptions.IgnoreCase)
                    select tp;
            if (t.Count() == 0)
                return PageTypeEnum.None;
            else
                return t.First().PageType;
            //pages.Select(reg =>Regex.IsMatch(uri, reg.MatchRegex));
            //return PageType.BookContent;
        }
        public class TypeRegex
        {
            public TypeRegex(PageTypeEnum type, string regex)
            {
                PageType = type;
                MatchRegex = regex;
            }
            public PageTypeEnum PageType { get; set; }
            public string MatchRegex { get; set; }
        }
    }
    public enum PageTypeEnum
    {
        None,
        BookPage,
        BookContent,
        Chapter
    }
}
